<?php

class Application_Model_Funcionario extends Zend_Db_Table_Abstract {

    protected $_name = 'funcionario';
    protected $_primary = 'codigo';
    protected $_dependentTables = array('');
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_Funcionario();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insere($data){
        if(isset($data['dt_contratacao']) && isset($data['salario']) && isset($data['username']) 
                && isset($data['codigo']) && isset($data['password'])){
            $data['password'] = sha1($data['password']);
            parent::insert($data);
        }else{
            new Exception("não inseriu funcionario");
        }
    }
    
    public function update($data){
        if(isset($data['dt_contratacao']) && isset($data['salario']) && isset($data['username']) 
                && isset($data['codigo']) && isset($data['password'])){
            if(isset($data['password'])){
                $data['password'] = sha1($data['password']);
            }else{
                $data['password'] = null;
            }
            $where ='codigo = '.$data['codigo'];
            parent::update($data, $where);
        }else{
            new Exception("não atualizou funcionario");
        }
        
    }

    public function getListFuncionarios(){
        $consultaSql = 'select username, codigo from funcionario where dt_exclusao is null';
        return $this->getAdapter()->query($consultaSql);
    }

    public function getByUsername($username,$password) {
        $select = $this->getAdapter()->select();
        $select->from(
                    array('f' => $this->_name),
                    array(  'f.codigo','f.perfil_acesso'
                    ),
                    $this->_schema
                )
               ->join(array('p' => 'pessoa'), 'p.codigo = f.codigo',array('p.*'))
               ->join(array('pa' => 'perfil_acesso'), 'f.perfil_acesso = pa.codigo',array('pa.descricao as perfil'))
               ->where('p.dt_exclusao is null')
               ->where('f.dt_exclusao is null')
               ->where('f.username = \''.$username.'\'');
        if(isset($password)){
            $select->where('f.password = \''.sha1($password).'\'');
        }
        
        return $this->getAdapter()->fetchAll($select);
    }

    public function getByid($id){
        return $this->fetchRow("codigo = " . $id ." and dt_exclusao is null");
    }
    
    public function remove($id){
        $func = $this->getByid($id)->toArray();
        if(isset($func) && !is_null($func)){
            $where = ' codigo = ' .$id;
            $func['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
            parent::update($func, $where);    
        }
    }
    
    public function getQntFuncionarioByPerfilAcesso($perfilAcesso){
        $sql = 'select count(codigo) as qnt from funcionario where dt_exclusao and perfil_acesso ='.$perfilAcesso;
        return $this->getAdapter()->fetchRow($sql);
    }
}